package com.fang.networkdisk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fang.common.common.CommonPage;
import com.fang.common.exception.ExceptionCode;
import com.fang.common.exception.NetworkDiskException;
import com.fang.networkdisk.entity.bo.UserRoleRelationBo;
import com.fang.networkdisk.entity.po.TbNetDisUserRoleRelation;
import com.fang.networkdisk.entity.vo.UserRoleRelationVo;
import com.fang.networkdisk.mapper.TbNetDisUserRoleRelationMapper;
import com.fang.networkdisk.service.TbNetDisUserRoleRelationService;
import com.fang.networkdisk.util.NDBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户角色关联关系表 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2024-12-07
 */
@Service
public class TbNetDisUserRoleRelationServiceImpl extends ServiceImpl<TbNetDisUserRoleRelationMapper, TbNetDisUserRoleRelation> implements TbNetDisUserRoleRelationService {

    @Resource
    private TbNetDisUserRoleRelationMapper tbNetDisUserRoleRelationMapper;

    @Override
    public CommonPage queryUserRoleRelation(UserRoleRelationBo userRoleRelationBo) {
        userRoleRelationBo.setOffset((userRoleRelationBo.getCurPage() - 1) * userRoleRelationBo.getPageSize());
        List<UserRoleRelationVo> userRoleRelations = this.tbNetDisUserRoleRelationMapper.queryUserRoleRelation(userRoleRelationBo);
        int total = this.tbNetDisUserRoleRelationMapper.queryUserRoleRelationCount(userRoleRelationBo);
        CommonPage result = new CommonPage(userRoleRelationBo.getCurPage(), total, userRoleRelations);
        return result;
    }

    @Override
    public TbNetDisUserRoleRelation findUserRoleRelation(UserRoleRelationBo userRoleRelationBo) {
        QueryWrapper<TbNetDisUserRoleRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userRoleRelationBo.getUserId())
                .eq("role_id", userRoleRelationBo.getRoleId());
        TbNetDisUserRoleRelation tbNetDisUserRoleRelation = this.tbNetDisUserRoleRelationMapper.selectOne(wrapper);
        return tbNetDisUserRoleRelation;
    }

    @Override
    public int addUserRoleRelation(UserRoleRelationBo userRoleRelationBo) {
        TbNetDisUserRoleRelation db_UserRoleRelation = findUserRoleRelation(userRoleRelationBo);
        if (NDBeanUtils.isObjectNotEmpty(db_UserRoleRelation)) {
            throw new NetworkDiskException(ExceptionCode.DATA_DUPLICATE);
        }
        TbNetDisUserRoleRelation userRoleRelation = new TbNetDisUserRoleRelation();
        NDBeanUtils.copyListProperties(userRoleRelationBo, userRoleRelation);
        return tbNetDisUserRoleRelationMapper.insert(userRoleRelation);
    }

    @Override
    public int updateUserRoleRelationById(UserRoleRelationBo userRoleRelationBo) {
        TbNetDisUserRoleRelation userRoleRelation = new TbNetDisUserRoleRelation();
        NDBeanUtils.copyListProperties(userRoleRelationBo, userRoleRelation);
        return tbNetDisUserRoleRelationMapper.updateById(userRoleRelation);
    }

    @Override
    public int deleteUserRoleRelationById(Integer id, String authorId) {
        UpdateWrapper<TbNetDisUserRoleRelation> wrapper = new UpdateWrapper<>();
        wrapper.set("update_user", authorId)
                .set("updated_time", new Date())
                .set("deleted", true)
                .eq("id", id);
        return tbNetDisUserRoleRelationMapper.update(null, wrapper);
    }

}
